//
//  SYueduViewController.m
//  News
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 PEAK. All rights reserved.
//

#import "SYueduViewController.h"
#import "SNetWorkHandle.h"
#import "SReadxiangqing.h"
#import "SReadxiangFirst.h"

#import "SDataBaseManager.h"
@interface SYueduViewController ()<UIWebViewDelegate>

@property (nonatomic, retain) UIWebView *web;

@property(nonatomic, retain) UIActivityIndicatorView *activityIndicator;


@property(nonatomic, retain) NSMutableArray *biaotiArr;

@property(nonatomic, retain) UIAlertView *al;

@property(nonatomic, retain) NSString *body;



@property(nonatomic, retain) UIImageView *MyView;
@end

@implementation SYueduViewController



- (void)dealloc
{
    
    
    _web.delegate = nil;
    [_web release];
    
    [_activityIndicator release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

 self.title = @"详情";
    self.view.backgroundColor = [UIColor whiteColor];
    self.biaotiArr = [NSMutableArray array];
    
  
    
    //self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(shoucangAA)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-guanwangicon31332-2"] style:UIBarButtonItemStylePlain target:self action:@selector(shoucangAA)];
    self.web = [[UIWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_web];
    _web.delegate = self;
    _web.scrollView.bounces = NO;
   [_web release];
    
    [_activityIndicator startAnimating];
    [self getData1];
   
    
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(fanhui)];
    
   
}
- (void)fanhui
{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)setStr:(NSString *)str
{
    if (_str != str) {
        [_str release];
        _str = [str retain];
    }
    [self getData];
    
}


- (void)getData1
{

     [_web loadHTMLString:_xiangxi baseURL:nil];
    
}


- (void)shoucangAA
{
    SDataBaseManager *dbManager = [SDataBaseManager shareInstance];
    //打开数据库
    [dbManager openDB];
    //创建表
    [dbManager createTable];
    //添加对象
    SReadshoucang *shoucang = [[[SReadshoucang alloc] init] autorelease];
    shoucang.title = self.biaoti;
    shoucang.body = self.xiangxi;
    shoucang.ref = self.ref;
    shoucang.src = self.src;
   
    NSArray *arr = [NSArray array];
    arr = [dbManager selectInfo];
   
    for (SReadshoucang *shoucang in arr) {
        [_biaotiArr addObject:shoucang.title];
        
    }
    if (![_biaotiArr containsObject:shoucang.title]) {
        [dbManager insertInfoWithSReadshoucang:shoucang];
        
        self.al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [self.view addSubview:_al];
        [_al show];
        [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(lisong) userInfo:nil repeats:NO];
    }else {
        self.al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [self.view addSubview:_al];
        [_al show];
    }

}
- (void)lisong
{
    [_al dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)getData
{
    self.MyView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    
    _MyView.image = [UIImage imageNamed:@"5EA2D64D-15BE-43D8-AB18-E15BCE039C8F"];
    _MyView.tag = 1000;
    [_MyView setBackgroundColor:[UIColor blackColor]];
    _MyView.alpha = 1;
    [self.view addSubview:_MyView];
    [_MyView release];
    _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [_activityIndicator setCenter:_MyView.center];
    [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [_MyView addSubview:_activityIndicator];
    [_activityIndicator startAnimating];
    
    
    
    NSString *str = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/%@/full.html", self.str];
    NSLog(@"%@", str);
    [SNetWorkHandle getDataWithURL:str completion:^(id result) {
        NSMutableDictionary *dic = [result objectForKey:_str];
        SReadxiangqing *xiangqing = [[SReadxiangqing alloc] initWithDic:dic];
        self.biaoti = xiangqing.title;
        
       // self.title = xiangqing.title;
        
        NSString *str = [NSString stringWithFormat:@"<h1>%@</h1>", xiangqing.title];
        self.body = [str stringByAppendingString:xiangqing.body];
        //xiangqing.imgArr = [NSMutableArray array];
        for (NSDictionary *dic in xiangqing.img) {
            
            
            SReadxiangFirst *srf = [[SReadxiangFirst alloc] initWithDic:dic];
            NSArray *arr = [srf.pixel componentsSeparatedByString:@"*"];
            srf.width = arr[1];
            srf.height = arr[0];
            [xiangqing.imgArr addObject:srf];
            [srf release];
            
        }
        
        
        
        
        //if ( ([xiangqing.Stemplate isEqualToString:@"normal1"] && xiangqing.source_url != nil )|| [xiangqing.Stemplate isEqualToString:@"normal2"]) {
        
        //            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:xiangqing.source_url]];
        //         [_web loadRequest:request];
        //
        //} else if([xiangqing.Stemplate isEqualToString:@"normal"] || ([xiangqing.Stemplate isEqualToString:@"normal1"] && xiangqing.source_url == nil)) {
        
        if (xiangqing.img.count != 0) {
            
        
        for (SReadxiangFirst *srf in xiangqing.imgArr) {
            
            self.ref = srf.ref;
            self.src = srf.src;
            NSString *str = [NSString stringWithFormat:@"<img width=%lf src=\"%@\">", self.view.bounds.size.width - 20,self.src];
            self.body = [self.body stringByReplacingOccurrencesOfString:srf.ref withString:str];
        }
        self.xiangxi = self.body;
        [_web loadHTMLString:self.body baseURL:nil];
        
        }
        // }
        else{
            self.biaoti = xiangqing.title;
          NSString *str = [NSString stringWithFormat:@"<h1>%@</h1>", xiangqing.title];
            self.xiangxi = xiangqing.body;
            xiangqing.body = [str stringByAppendingString:xiangqing.body];
            [_web loadHTMLString:xiangqing.body baseURL:nil];
           
        }
        


        


        _MyView.hidden = YES;
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
